Ext.onReady(function(){
    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';
    var pagesize = 22;	
    var openUrl = basePath
    + 'JarpListener?className=cn.com.jsepc.rep.report.FillDataDAO';
    openUrl += '&methodName=getDataGrid&paramCount=5&param_2='+repid+'&param_3='+corpid+'&param_4='+getDemlist(); 
 
	var modalwin_t = new Ext.Window({
	    id:'modalwin_t',
	    title:"维度信息",
	    border:false,
	    bodyBorder:false,
	    layout:'fit',
	    width:500,
	    height:510,
	    closeAction:'hide',
//	    plain: true,
		modal: true,
		tbar:['-',{
		            text: ' 确定 ',
		            handler: function(){
		            	selectItem();
		            	modalwin_t.hide();
			        }
		       },'-',{
		            text: ' 关闭 ',
		            handler: function(){
			        	modalwin_t.hide();
			        }
		       },'-'],
        items:[p_wdxx]
	});
       
    
	
    var store = new Ext.data.Store({    
    	proxy : new Ext.data.HttpProxy( {
            url : openUrl
        }),	       
        reader : jReader1
    });    
 var sm = new Ext.grid.RowSelectionModel({
	singleSelect : true
});   
cm.defaultSortable = true;
var grid = new Ext.grid.EditorGridPanel({
		id:'grid_t',
       // region : 'center',
		anchor:'100% 95%',
       // renderTo:'qrgridDiv',
	    store:store,
	    sm :sm,
	    cm:cm,  
        viewConfig : {
            forceFit : false
        },
        frame : true,
        stripeRows: true, //显示行的分隔符
        loadMask:true,//数据加载时的遮罩功能 
        columnLines : true,
        clicksToEdit: 1,
        autoScroll:true, //控制滚动条
        height:screen.height-400,
        stateful : true,
        stateId : 'grid_t',
        bbar : new Ext.PagingToolbar({
	        pageSize: pagesize,
	        store: store,
	        paramNames : {
	            start : 'param_0',
	            limit : 'param_1'
	        },
	        displayInfo: true,
	        emptyMsg : '没有数据需要显示&nbsp;&nbsp;'
	    })
    }); 
    
 /*store.load({params : {
                param_0:0,
                param_1:pagesize
            }});*/
    
function getDemlist(){
	var sender={};
    var array=[];
	for (var i=0;i<dimLen;i++){
		var o={};
		o.facdimtable = Ext.ComponentMgr.get("facdimtable"+i).getValue();
		o.facdimcodecol = Ext.ComponentMgr.get("facdimcodecol"+i).getValue();
		o.facdimvalue = Ext.ComponentMgr.get("facdimvalue"+i).getValue();
		//demArr[i]  = o;
		array.push(o);
	}
	sender.demlist = array;
	sender.iswcz=Ext.getCmp("iswcz").getValue();
	//alert(Ext.util.JSON.encode(sender));
	return Ext.util.JSON.encode(sender);
}

   
   
  
   //主视图
    var viewport = new Ext.Viewport({
        layout : 'border',
        items:[{
            tbar : ['-', {
                text : '维度过滤',
                id:'btn1',
                handler : function() {
                	modalwin_t.show();
                }
            },'-',{
            	text : '导出excel',
                 id:'btn2',
                handler : function() {
                    selectItem_1();				 
                }
            }, '-', {
                text : '关闭',
                handler : function() {
                    window.close();
                }
            }, '-', {
                    xtype:'hidden',
                    id:'btn'
                }
            ],
            region : 'center',
            deferredRender : false,
            frame : true,
            layout : 'border',
            
            items:[
            {
                region : 'center',
                layout:'anchor',
                deferredRender : false,
                frame : true,
               // width:600,
               // html : "<div id='qrgridDiv'></div>",
                items:[grid]
            }
            ]
        }]

    });
    
    
    

//回车换行
grid.on("afteredit",function(e){
    if(e.row<grid.getStore().getCount()){ 
        var delay = new Ext.util.DelayedTask(function(){
            grid.startEditing(e.row+1,e.column);
        });
	delay.delay(50);

    }
});

   

//查询
function selectItem(){       
    var iswcz=Ext.getCmp("iswcz").getValue();
    //未勾选完成值
    if(!iswcz){
	    var openUrl = basePath
	    + 'JarpListener?className=cn.com.jsepc.rep.report.QueryData';
	    openUrl += '&methodName=getDataGrid&paramCount=5&param_2='+repid+'&param_3='+corpid+'&param_4='+getDemlist(); 
             hideColumn([]);
    }else{//勾选完成值
    	var hideCol=[];
    	var array=[];
    	var sender={};
    	for (var i=0;i<dimLen;i++){
			var o={};
			o.facdimtable = Ext.ComponentMgr.get("facdimtable"+i).getValue();
			o.facdimcodecol = Ext.ComponentMgr.get("facdimcodecol"+i).getValue();
			o.facdimvalue = Ext.ComponentMgr.get("facdimvalue"+i).getValue();
			o.facdimfield=Ext.ComponentMgr.get("facdimfield"+i).getValue();
			if(o.facdimvalue==""){
				o.facdimvalue="99999";
				hideCol.push(o.facdimfield);
			}
			array.push(o);
		}
		sender.demlist = array;
		sender.iswcz=Ext.getCmp("iswcz").getValue();
		
		//如果勾选完成值，又一个维度都没有勾选，给出提示
		if(hideCol.length==dimLen){
		alert("请至少选择一个维度过滤条件。");
			return;
		}
		hideColumn(hideCol);
		 var openUrl = basePath
	    + 'JarpListener?className=cn.com.jsepc.rep.report.FillDataDao_Wan';
	    openUrl += '&methodName=getDataGrid&paramCount=5&param_2='+repid+'&param_3='+corpid+'&param_4='+Ext.util.JSON.encode(sender);
    }
     store.proxy = new Ext.data.HttpProxy( {
            url : openUrl
        });
     store.load({params : {
        param_0:0,
        param_1:pagesize
    }});
}

function selectItem_1(){  
	var iswcz=Ext.getCmp("iswcz").getValue();
	if(!iswcz){
	    var openUrl = basePath
	    + '/servlet/ExportServlet?iswcz=false&';
	    openUrl += 'repid='+repid+'&corpid='+corpid+'&getDemlist='+getDemlist(); 
	  // httpR_itemInfo = getXMLHttpRequest();
	     //   httpR_itemInfo.open("POST", openUrl, true);
	      //  httpR_itemInfo.send(); 
	      window.open(openUrl);
    }else{
    	var array=[];
    	var sender={};
    	for (var i=0;i<dimLen;i++){
			var o={};
			o.facdimtable = Ext.ComponentMgr.get("facdimtable"+i).getValue();
			o.facdimcodecol = Ext.ComponentMgr.get("facdimcodecol"+i).getValue();
			o.facdimvalue = Ext.ComponentMgr.get("facdimvalue"+i).getValue();
			o.facdimfield=Ext.ComponentMgr.get("facdimfield"+i).getValue();
			if(o.facdimvalue==""){
				o.facdimvalue="99999";
			}
			array.push(o);
		}
		sender.demlist = array;
		sender.iswcz=Ext.getCmp("iswcz").getValue();
		
		 var openUrl = basePath
	    + '/servlet/ExportServlet?';
	    openUrl += 'iswcz=true&repid='+repid+'&corpid='+corpid+'&getDemlist='+Ext.util.JSON.encode(sender); 
	    window.open(openUrl);
    }
}
    
    
 
  //设置列隐藏
function hideColumn(hiddenCols){
	var columnModel=grid.getColumnModel();
	for(var i=0;i<columnModel.getColumnCount();i++){
		var dataIndex=columnModel.getDataIndex(i);
		var flag=false;
		for(var j=0;j<hiddenCols.length;j++){
			if(dataIndex.indexOf(hiddenCols[j])>=0){
				flag=true;
				break;
			}
		}
		//如果需要隐藏的列
		if(flag){
			columnModel.setHidden(i,true);
		}else{
			var header=columnModel.getColumnHeader(i);
			if(header.indexOf("ID")>=0){
				columnModel.setHidden(i,true);
			}else{
				columnModel.setHidden(i,false);
			}
		}
	}
}
selectItem();
});